package com.huawei.springboot2023.business.goods.controller;


import com.huawei.springboot2023.business.goods.dto.Goods;
import com.huawei.springboot2023.business.goods.service.IGoodsService;
import com.huawei.springboot2023.core.dto.MyPageBand;
import com.huawei.springboot2023.core.dto.MyPager;
import com.huawei.springboot2023.core.dto.MyResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhouChengYang
 * @since 2023-04-07
 */
@RestController
@Slf4j
@RequestMapping("/goods")
public class GoodsController {
    @Resource(name = "goodsServiceImpl")
    private IGoodsService goodsService;

    @GetMapping("/page")
    @ApiOperation("物资台账分页查询")
    public MyResult page(Goods goods, MyPageBand pageBand){
        log.debug("--台账管理模块：物资台账分页查询");

        MyResult result = new MyResult();

        try {
            MyPager<Goods> res = goodsService.myPage(goods,pageBand);

            result.setMsg("查询成功！");
            result.setData(res);
        }catch (Exception e){
            result.setMsg("分页查询异常！");
            result.setCode("9999");
        }


        return result;

    }

    @PostMapping("/add")
    @ApiOperation("台账--新增")
    public MyResult add(@RequestBody Goods goods){

        MyResult result = new MyResult();
        try {
            boolean save = goodsService.save(goods);
            if(save){
                result.setMsg("添加成功！");
            }else{
                result.setMsg("添加失败！");
                result.setCode("9999");
            }
        }catch (Exception e){

            e.printStackTrace();
            result.setMsg("新增异常！");
            result.setCode("9999");
        }


        return result;

    }

}
